import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    schoolId: Number(sessionStorage.getItem("schoolId")) || 0,
    classId: Number(sessionStorage.getItem("classId")) || null,
    hkSchoolId: sessionStorage.getItem("hkSchoolId") || ''
  },
  getter: {
    getSchoolId: state => {
      return state.schoolId;
    },
    getClassId: state => {
      return state.classId;
    },
    getHkSchoolId : state => {
      return state.hkSchoolId;
    }
  },
  mutations: {
    changeSchoolId(state, schoolId) {
      return (state.schoolId = schoolId);
    },
    changeClassId(state, classId) {
      return (state.classId = classId);
    },
    changeHkSchoolId(state, hkSchoolId) {
      return (state.hkSchoolId = hkSchoolId)
    }
  },
  actions: {
    actionSchoolId(context, schoolId) {
      sessionStorage.setItem("schoolId", schoolId);
      return context.commit("changeSchoolId", schoolId);
    },
    actionClassId(context, classId) {
      sessionStorage.setItem("classId", classId);
      return context.commit("changeClassId", classId);
    },
    actionHkSchoolId(context, hkSchoolId) {
      sessionStorage.setItem("hkSchoolId", hkSchoolId)
      return context.commit("changeHkSchoolId", hkSchoolId)
    }
  }
});
